package iterator;

import item.*;
import attribute.*;

import java.lang.reflect.*;
import java.util.*;


/**
 * @author John Breyssem	1Ba Informatica		r0303394
 * 
 * @author Lars Feyaerts	1Ba Informatica		r0296831
 * 
 * @version	1.3
 *
 */
public class ConditionIterator implements Iterator<Item> {

        private Vector<Item> list;
        private int currentPosition;

        /**
         * @param board		
         * 
         * @param condition	
         * 
         * @param param		
         * 
         */
        public ConditionIterator(Board board,Method condition, Object param) {
                list = new Vector<Item>();
                currentPosition = 0;
                for (Item item : board.getListOfItemsOnBoard()) {
                        try {   
                                if ((Boolean) condition.invoke(item,param)) { 
                                        list.add(item);
                                }
                        } catch (IllegalAccessException e1) {
                                e1.printStackTrace();
                        } catch (IllegalArgumentException e1) {
                                e1.printStackTrace();
                        } catch (InvocationTargetException e1) {
                                e1.printStackTrace();
                        }
                }
        }

        @Override
        public boolean hasNext() {
                return currentPosition < list.size();
        }

        @Override
        public Item next() {
                  Item item = list.elementAt(currentPosition);
                  currentPosition = (currentPosition + 1) % list.size(); 
                  return item;
        }

        @Override
        public void remove() {
                list.remove(list.elementAt(currentPosition));
        }

}

